package com.hjqjl.module_home.ui.list;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hjqjl.module_home.databinding.HomeActivityMyexpandablelistviewBinding;
import com.hjqjl.module_home.R;
import com.hjqjl.whapplib.base.BaseActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


public class MyExpandableListViewActivity extends BaseActivity<HomeActivityMyexpandablelistviewBinding> {
    private List<String> parentData;
    private Map<String, List<String>> childDataMap;

    public static void start(Context context) {
        Intent starter = new Intent(context, MyExpandableListViewActivity.class);
        context.startActivity(starter);
    }


    @Override
    public void initData() {
        setTitle("MyExpandableListViewActivity");
        init();

        pVBinding.homeExLvMyExpandableListView.setOnGroupExpandListener(groupPosition -> {
            for (int i = 0, count = pVBinding.homeExLvMyExpandableListView.getExpandableListAdapter().getGroupCount(); i < count; i++) {
                if (groupPosition != i) {// 关闭其他分组
                    pVBinding.homeExLvMyExpandableListView.collapseGroup(i);
                }
            }
        });
    }

    @Override
    public void processLogic() {
        pVBinding.homeExLvMyExpandableListView.setAdapter(new MyExpandableListViewAdapter(pContext, parentData, childDataMap));
    }


    // 初始化数据
    private void init() {
        parentData = new ArrayList<>();
        childDataMap = new HashMap<>();

        addInfo("1、111111111？", new String[]{"11111111111"});
        addInfo("2、222222？", new String[]{"222", "22222", "2222"});
        addInfo("3、3333333333？", new String[]{"3333", "3333333"});
        addInfo("4、444444444444？", new String[]{"44444", "44444"});
        addInfo("5、5555555555？", new String[]{"555555555"});

    }

    /**
     * 给组、子列表添加数据
     *
     * @param p -parentData
     * @param c -child
     */
    private void addInfo(String p, String[] c) {
        parentData.add(p);
        List<String> childItem = new ArrayList<>();
        Collections.addAll(childItem, c);
        childDataMap.put(p, childItem);
    }

    private class MyExpandableListViewAdapter extends BaseExpandableListAdapter {
        private Context mContext;
        private List<String> mParentData;
        private Map<String, List<String>> mChildDataMap;


        MyExpandableListViewAdapter(Context context, List<String> parentData, Map<String, List<String>> childDataMap) {
            this.mContext = context;
            this.mParentData = parentData;
            this.mChildDataMap = childDataMap;
        }

        @Override
        public int getGroupCount() {
            return mParentData.size();
        }

        // 获取当前父item下的子item的个数
        @Override
        public int getChildrenCount(int groupPosition) {
            String key = mParentData.get(groupPosition);
            return Objects.requireNonNull(mChildDataMap.get(key)).size();
        }

        // 获取当前父item的数据
        @Override
        public Object getGroup(int groupPosition) {
            return mParentData.get(groupPosition);
        }

        // 得到子item需要关联的数据
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            String key = mParentData.get(groupPosition);
            return (Objects.requireNonNull(mChildDataMap.get(key)).get(childPosition));
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        // 得到子item的ID
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        //是否指定分组视图及其子视图的ID对应的后台数据改变也会保持该ID.
        @Override
        public boolean hasStableIds() {
            return true;
        }

        // 设置父item组件
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            MyExpandableListViewActivity.MyExpandableListViewAdapter.ViewHolder holder;
            if (convertView == null) {
                holder = new MyExpandableListViewActivity.MyExpandableListViewAdapter.ViewHolder();
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.home_item_myexpandableistview_parent, parent, false);
//				convertView = View.inflate(context, R.layout.question_expandable_parent, null);//这种方法字体样式没变
                holder.parentTv = convertView.findViewById(R.id.home_tv_myExpandableListView_item_parent);
                holder.parentImageViw = convertView.findViewById(R.id.home_iv_myExpandableListView_item_parent);
                convertView.setTag(holder);
            } else {
                holder = (MyExpandableListViewActivity.MyExpandableListViewAdapter.ViewHolder) convertView.getTag();
            }
            holder.parentTv.setText(MyExpandableListViewActivity.this.parentData.get(groupPosition));
            // 判断isExpanded就可以控制是按下还是关闭，同时更换图片
            if (isExpanded) {
                holder.parentImageViw.setBackgroundResource(R.drawable.ic_arrow_up);
            } else {
                holder.parentImageViw.setBackgroundResource(R.drawable.ic_arrow_down);
            }

            return convertView;
        }

        // 设置子item的组件
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            String key = MyExpandableListViewActivity.this.parentData.get(groupPosition);
            String info = Objects.requireNonNull(mChildDataMap.get(key)).get(childPosition);
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.home_item_myexpandablelistview_children, parent, false);
            }
            TextView tv = convertView.findViewById(R.id.home_tv_myExpandableListView_item_content);
            tv.setText(info);
            return convertView;
        }

        //而isChildSelectable(int groupPosition, int childPosition)用来判断某Group某个child是否可可选。
        //我们可以添加条件控制某Group某个child可点或不可点击。当不加任何条件直接返回false,所有的组的child均不可点击。
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        class ViewHolder {
            private TextView parentTv;
            private ImageView parentImageViw;
        }
    }
}
